<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/3/12
 * Time: 1:45
 */


//统计已经报名人数
function sumPeo($enid, $enclass)
{
    if (!empty($enid) && isset($enclass)) {
        if ($enclass == 1) {
            $stuenroll = M("stuenroll");
            $num = $stuenroll->where("enid='{$enid}'")->count();
        } else {
            $team = M("team");
            $num = $team->where("enid='{$enid}'")->count();
        }
        return $num;
    }
}

//通过赛项编号获取赛项最大报名人数
function EnidToNum($enid)
{
    if (isset($enid)) {
        $EnrollInfo = M("enrollinfo");
        $ret = $EnrollInfo->where("enid={$enid}")->find();
        return $ret["num"];
    }
}

//通过赛项编号获取赛项类别
function EnidToClass($enid)
{
    if (isset($enid)) {
        $EnrollInfo = M("enrollinfo");
        $ret = $EnrollInfo->where("enid={$enid}")->find();
        return $ret["enclass"];
    }

}

//检测是否已经报名
function Is_Enrolled($stuid, $enclass, $enid)
{
    if (!empty($stuid) && isset($enclass)) {
        if ($enclass == 1) {
            $stuenroll = M("stuenroll");
            $map["stuid"] = $stuid;
            $map["enid"] = $enid;
            $num = $stuenroll->where($map)->count();
        } else {
            $teaminfo = M("teaminfo");
            $team = M("team");
            $num = $teaminfo->where("stuid={$stuid}")->count();
            if ($num > 0) {
                $res = $teaminfo->where("stuid={$stuid}")->select();
                foreach ($res as $vo) {
                    $map["teamid"] = $vo["teamid"];
                    $map["enid"] = $enid;
                    $num = $team->where($map)->count();
                    if ($num > 0) {
                        return $num;
                    }
                }
                return 0;
            } else {
                return $num;
            }
        }
        return $num;

    }


}

//通过学号获取学生姓名
function getStuName($stuid)
{
    if (!empty($stuid)) {
        $StuInfo = M("stuinfo");
        $ret = $StuInfo->where("stuid=$stuid")->find();
        return $ret["name"];
    }
}

//通过团队编号获取学号
function getEnrStuid($teamid)
{
    if (!empty($teamid)) {
        $Teaminfo = M("teaminfo");
        $ret = $Teaminfo->where("teamid=$teamid")->find();
        return $ret["stuid"];
    }

}

//通过团队编号获取团队赛项名称
function getTeamEnName($teamid)
{
    if (!empty($teamid)) {
        $Team = M("team");
        $enid = $Team->where("teamid=$teamid")->getField("enid");
        return getEnrName($enid);
    }

}

//通过赛项编号取得赛项姓名
function getEnrName($enid)
{
    if (!empty($enid)) {
        $EnInfo = M("enrollinfo");
        $ret = $EnInfo->where("enid=$enid")->find();
        return $ret["title"];
    }

}

//通过系部编号取得系部名称
function getDeptName($deptid)
{
    if (!empty($deptid)) {
        $deptinfo = M("deptinfo");
        $ret = $deptinfo->where("did=$deptid")->find();
        return $ret["name"];
    }
}

//通过班级编号取得班级名称
function getClassName($classid)
{
    if (!empty($classid)) {
        $classinfo = M("classinfo");
        $ret = $classinfo->where("cid=$classid")->find();
        return $ret["name"];
    }

}

//团队所有成员姓名
function getTeamName($teamid)
{
    if (!empty($teamid)) {
        $TeamInfo = M("teaminfo");
        $ret = $TeamInfo->where("teamid=$teamid")->select();
        foreach ($ret as $vo) {
            $name[] = getStuName($vo[stuid]);
        }
        return implode("、", $name);
    }
}

//获取所有系部
function getDept()
{
    $DeptInfo = M("deptinfo");
    return $DeptInfo->select();
}

//获取比赛类别
function getMatchClass($enclass)
{
    if (isset($enclass)) {
        switch ($enclass) {
            case 1:
                return "个人";
                break;
            case 0:
                return "团队";
                break;
        }
    }
}

//权限名称
function getRankName($rank)
{
    if (isset($rank)) {
        switch ($rank) {
            case 1:
                return "高权";
                break;
            case 2:
                return "普通";
                break;
        }
    }
}

//获取权限值
function getRankId($RankName)
{
    if (!empty($RankName)) {
        switch ($RankName) {
            case "管理员":
                return 0;
                break;
            case "高权":
                return 1;
                break;
            case "普通":
                return 2;
                break;
        }
    }
}

//获取系部编号
function getDeptId($name)
{
    $DeptInfo = D("DeptInfo");
    $deptid = $DeptInfo->where("name='" . $name . "'")->getField("did");
    return $deptid;
}

//获取班级编号
function getClassId($name)
{
    $ClassInfo = D("ClassInfo");
    $classid = $ClassInfo->where("name='" . $name . "'")->getField("cid");
    return $classid;
}

//导入excel
function use_upload()
{
    $config = array(
        'maxSize' => 20971520,
        'rootPath' => './UPLOADS/',
        'savePath' => '',
        'saveName' => array('uniqid', ''),
        'exts' => array('csv', 'xls', 'xlsx'),
        'autoSub' => true,
        'subName' => array('date', 'Ymd'),
    );
    $upload = new \Think\Upload($config);

    $info = $upload->upload();
    if (!$info) {
        $this->error($upload->getError());
    } else {
        /*
        foreach ($info as $file) {
            echo $file['savepath'] . $file['savename'];
        }*/
        return $info;
    }
}

//通过学号获取系部
function StuIdToDept($stuid)
{
    if (!empty($stuid)) {
        $StuInfo = M("stuinfo");
        $DeptId = $StuInfo->where("stuid=$stuid")->getField("deptid");
        return getDeptName($DeptId);
    }
}

//通过学号获取班级
function StuIdToClass($stuid)
{
    if (!empty($stuid)) {
        $StuInfo = M("stuinfo");
        $ClassId = $StuInfo->where("stuid=$stuid")->getField("classid");
        return getClassName($ClassId);
    }
}

//通过学号获取性别
function StuIdToSex($stuid)
{
    if (!empty($stuid)) {
        $StuInfo = M("stuinfo");
        $sex = $StuInfo->where("stuid=$stuid")->getField("sex");
        if ($sex == 1) {
            return "男";
        } else {
            return "女";
        }
    }
}

//生成赛项编号
//160501601001
//16 05 01 601 001
//16=>年份
//05=>月份
//01=>系部编号后两位
//601=>创建本赛项人ID后三位
//001=>编号
function SetEnId($DeptId, $UserId)
{
    if (isset($DeptId) && isset($UserId)) {
        $a = date("y", time());
        $b = date("m", time());
        $c = substr($DeptId, -2);
        $d = substr($UserId, -3);
        $tmp = $a . $b . $c . $d;
//        $tmp = $a . $b . $c ;
        $EnrollInfo = M("enrollinfo");
        $map["enid"] = array("like", $tmp . "%");
        $ret = $EnrollInfo->where($map)->max("enid");
        if (isset($ret)) {
            return $ret + 1;
        } else {
            return $tmp . '001';
        }
    }
}

//获得系部学生学号
function getDeptStuid($deptid)
{
    if (!empty($deptid)) {
        $StuInfo = M("stuinfo");
        $stuid = $StuInfo->where("deptid='$deptid'")->select();
        return $stuid;
        //return implode(",", $ret);
    }

}

//获得系部报名数
function SumPeoDeptOne($enid, $deptid)
{
    $stuid = getDeptStuid($deptid);
    $sum = 0;
    $EnrollInfo = M('enrollinfo');
    $enclass = $EnrollInfo->where("enid=$enid")->getField("enclass");
    if ($enclass == 1) {
        $StuEnroll = M("stuenroll");
        foreach ($stuid as $vo) {
            $sumpeo = $StuEnroll->where("stuid='$vo[stuid]' and enid='$enid'")->count();
            if ($sumpeo) {
                $sum++;
            }
        }
    } else {
        foreach ($stuid as $vo) {
            $TeamInfo = M('teaminfo');
            $teamid = $TeamInfo->where("stuid='$vo[stuid]'")->select();
            foreach ($teamid as $vo1) {
                $Team = M('team');
                $sumpeo = $Team->where("teamid=$vo1[teamid] and enid='$enid'")->count();
                if ($sumpeo) {
                    $sum++;
                }
            }
        }
    }
    return $sum;
}

//获得所有系部报名数
function SumPeoDeptAll($enid)
{
    $DeptInfo = M("deptinfo");
    $ret = $DeptInfo->Field('did')->order('did')->select();
    foreach ($ret as $vo) {
        $sumPeo[] = SumPeoDeptOne($enid, $vo[did]);
    }
    return implode(",", $sumPeo);
}

//获得近三天报名数
function SumPeoDay($enid)
{
    $EnrollInfo = M('enrollinfo');
    $enclass = $EnrollInfo->where("enid={$enid}")->getField("enclass");
    if ($enclass == 1) {
        $StuEnroll = M('stuenroll');
        $firstday = strtotime("0 day");
        $secondday = strtotime("-1 day");
        $thirdday = strtotime("-2 day");
        $fourthday = strtotime("-3 day");
        $sumday[0] = $StuEnroll->where("addtime<=$thirdday and addtime>$fourthday and enid=$enid")->count();
        $sumday[1] = $StuEnroll->where("addtime<=$secondday and addtime>$thirdday and enid=$enid")->count();
        $sumday[2] = $StuEnroll->where("addtime<=$firstday and addtime>$secondday and enid=$enid")->count();
    } else {
        $Team = M('team');
        $firstday = strtotime("0 day");
        $secondday = strtotime("-1 day");
        $thirdday = strtotime("-2 day");
        $fourthday = strtotime("-3 day");
        $sumday[0] = $Team->where("addtime<=$thirdday and addtime>$fourthday and enid=$enid")->count();
        $sumday[1] = $Team->where("addtime<=$secondday and addtime>$thirdday and enid=$enid")->count();
        $sumday[2] = $Team->where("addtime<=$firstday and addtime>$secondday and enid=$enid")->count();
    }
    return implode(",", $sumday);
}

//获得本周报名数
function SumPeoWeek($enid)
{
    $EnrollInfo = M('enrollinfo');
    $enclass = $EnrollInfo->where("enid={$enid}")->getField("enclass");
    $minday = mktime(0, 0, 0, date('m'), date('d') - date('w'), date('Y'));
    $monday = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
    $tuesday = mktime(0, 0, 0, date('m'), date('d') - date('w') + 2, date('Y'));
    $wednesday = mktime(0, 0, 0, date('m'), date('d') - date('w') + 3, date('Y'));
    $thursday = mktime(0, 0, 0, date('m'), date('d') - date('w') + 4, date('Y'));
    $friday = mktime(0, 0, 0, date('m'), date('d') - date('w') + 5, date('Y'));
    $saturday = mktime(0, 0, 0, date('m'), date('d') - date('w') + 6, date('Y'));
    $sunday = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));

    if ($enclass == 1) {
        $StuEnroll = M('stuenroll');
        $sumday[0] = $StuEnroll->where("addtime<=$monday and addtime>$minday and enid=$enid")->count();
        $sumday[1] = $StuEnroll->where("addtime<=$tuesday and addtime>$monday and enid=$enid")->count();
        $sumday[2] = $StuEnroll->where("addtime<=$wednesday and addtime>$tuesday and enid=$enid")->count();
        $sumday[3] = $StuEnroll->where("addtime<=$thursday and addtime>$wednesday and enid=$enid")->count();
        $sumday[4] = $StuEnroll->where("addtime<=$friday and addtime>$thursday and enid=$enid")->count();
        $sumday[5] = $StuEnroll->where("addtime<=$saturday and addtime>$friday and enid=$enid")->count();
        $sumday[6] = $StuEnroll->where("addtime<=$sunday and addtime>$saturday and enid=$enid")->count();
    } else {
        $Team = M('team');
        $sumday[0] = $Team->where("addtime<=$monday and addtime>$minday and enid=$enid")->count();
        $sumday[1] = $Team->where("addtime<=$tuesday and addtime>$monday and enid=$enid")->count();
        $sumday[2] = $Team->where("addtime<=$wednesday and addtime>$tuesday and enid=$enid")->count();
        $sumday[3] = $Team->where("addtime<=$thursday and addtime>$wednesday and enid=$enid")->count();
        $sumday[4] = $Team->where("addtime<=$friday and addtime>$thursday and enid=$enid")->count();
        $sumday[5] = $Team->where("addtime<=$saturday and addtime>$friday and enid=$enid")->count();
        $sumday[6] = $Team->where("addtime<=$sunday and addtime>$saturday and enid=$enid")->count();
    }
    return implode(",", $sumday);
    //return $beginLastweek;
}

//计算报名百分比
function PeoPercent($pronum, $maxnum)
{
    $percent = ($pronum / $maxnum) * 100;
    return $percent;
}

//报名男数
//报名男数
function sumSexMan($enid)
{
    $EnrollInfo = M('enrollinfo');
    $enclass = $EnrollInfo->where("enid={$enid}")->getField("enclass");
    $sum = 0;
    if ($enclass == 1) {
        $StuEnroll = M("stuenroll");
        $stuid = $StuEnroll->where("enid={$enid}")->select();
        foreach ($stuid as $vo) {
            if (StuIdToSex($vo[stuid]) == "男") {
                $sum++;
            }
        }
    } else {
        $Team = M("team");
        $teamid = $Team->where("enid={$enid}")->select();
        foreach ($teamid as $vo) {
            $TeamInfo = M("teaminfo");
            $stuid = $TeamInfo->where("teamid={$vo[teamid]}")->select();
            foreach ($stuid as $vo) {
                if (StuIdToSex($vo[stuid]) == "男") {
                    $sum++;
                }
            }
        }
    }
    return $sum;
}

//报名女数
function sumSexWoman($enid)
{
    $EnrollInfo = M('enrollinfo');
    $enclass = $EnrollInfo->where("enid={$enid}")->getField("enclass");
    $sum = 0;
    if ($enclass == 1) {
        $StuEnroll = M("stuenroll");
        $stuid = $StuEnroll->where("enid={$enid}")->select();
        foreach ($stuid as $vo) {
            if (StuIdToSex($vo[stuid]) == "女") {
                $sum++;
            }
        }
    } else {
        $Team = M("team");
        $teamid = $Team->where("enid={$enid}")->select();
        foreach ($teamid as $vo) {
            $TeamInfo = M("teaminfo");
            $stuid = $TeamInfo->where("teamid={$vo[teamid]}")->select();
            foreach ($stuid as $vo) {
                if (StuIdToSex($vo[stuid]) == "女") {
                    $sum++;
                }
            }
        }
    }
    return $sum;
}

//输出Excel文件到浏览器
function browser_export($filename)
{
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
    header('Cache-Control: max-age=0');
}

function getGrade($Model,$enid){
    $map0["grade"] = array("eq", 0);

    $map1["grade"] = array(array("gt", 0),array("elt", 10));

    $map2["grade"] = array(array("gt", 10),array("elt", 20));

    $map3["grade"] = array(array("gt", 20),array("elt", 30));

    $map4["grade"] = array(array("gt", 30),array("elt", 40));

    $map5["grade"] = array(array("gt", 40),array("elt", 50));

    $map6["grade"] = array(array("gt", 50),array("elt", 60));

    $map7["grade"] = array(array("gt", 60),array("elt", 70));

    $map8["grade"] = array(array("gt", 70),array("elt", 80));

    $map9["grade"] = array(array("gt", 80),array("elt", 90));

    $map10["grade"] = array(array("gt", 90),array("lt", 100));

    $map11["grade"] = array("eq", 100);

    $g0 = $Model->where($map0)->where("enid=$enid")->order("grade")->count();
    $g1 = $Model->where($map1)->where("enid=$enid")->order("grade")->count();
    $g2 = $Model->where($map2)->where("enid=$enid")->order("grade")->count();
    $g3 = $Model->where($map3)->where("enid=$enid")->order("grade")->count();
    $g4 = $Model->where($map4)->where("enid=$enid")->order("grade")->count();
    $g5 = $Model->where($map5)->where("enid=$enid")->order("grade")->count();
    $g6 = $Model->where($map6)->where("enid=$enid")->order("grade")->count();
    $g7 = $Model->where($map7)->where("enid=$enid")->order("grade")->count();
    $g8 = $Model->where($map8)->where("enid=$enid")->order("grade")->count();
    $g9 = $Model->where($map9)->where("enid=$enid")->order("grade")->count();
    $g10 = $Model->where($map10)->where("enid=$enid")->order("grade")->count();
    $g11 = $Model->where($map11)->where("enid=$enid")->order("grade")->count();

    return $g0.",".$g1.",".$g2.",".$g3.",".$g4.",".$g5.",".$g6.",".$g7.",".$g8.",".$g9.",".$g10.",".$g11;
}